package way_dfs

import (
	. "whoops/leetcode/a690_员工的重要性/answer/common"
)

var sum int

func GetImportance(employees []*Employee, id int) int {
	sum = 0
	resultMap := make(map[int]*Employee, 0)
	for _, employee := range employees {
		resultMap[employee.Id] = employee
	}
	dfs(resultMap, resultMap[id])

	return sum
}

func dfs(resultMap map[int]*Employee, e *Employee) {
	if len(e.Subordinates) == 0 {
		sum += e.Importance
		return
	}
	for _, value := range e.Subordinates {
		dfs(resultMap, resultMap[value])
	}
	e.Subordinates = []int{}
	dfs(resultMap, resultMap[e.Id])
}
